package com.cqie.hospitalescort.controller;

import com.cqie.hospitalescort.DTO.ChatInfoDTO;
import com.cqie.hospitalescort.entity.OnlineChatInfo;
import com.cqie.hospitalescort.message.ChatMessage;
import com.cqie.hospitalescort.model.Result;
import com.cqie.hospitalescort.service.OnlineChatInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@RestController
@RequestMapping("/onlineChatInfo")
@Api(value = "聊天管理", tags = {"聊天管理"})
public class OnlineChatInfoController extends BaseController {

    @Autowired
    private OnlineChatInfoService chatInfoService;

    @PostMapping("/saveMessage")
    @ApiOperation(value = "发送消息")
    public Result<Void> saveMessage(@RequestBody ChatMessage message) {
        OnlineChatInfo chatInfo = new OnlineChatInfo();
        BeanUtils.copyProperties(message, chatInfo);
        chatInfo.setSenderType(String.valueOf(message.getSenderType()));
        chatInfoService.saveMessage(chatInfo);
        return response();
    }

    @GetMapping("/getMessages")
    @ApiOperation(value = "消息列表")
    public Result<List<ChatInfoDTO>> getMessages(@RequestParam String chatCode) {
        return response(chatInfoService.getMessagesByChatCode(chatCode));
    }

}
